﻿//////////////////////////////////////////////////
//                 Dragoon.cs                   //
//         Part of RebornCo by kaihaider        //
//////////////////////////////////////////////////
using System.Threading.Tasks;
using ff14bot;
using ff14bot.Enums;
using RebornCo.Helpers;
using RebornCo.Settings;
using TreeSharp;

namespace RebornCo.Rotations
{
    public class Dragoon : RebornCoRoutine
    {

        public override void OnInitialize()
        {
            WantHealing = false;
            WindowSettings = new LancerSetting();
        }
        public static LancerSetting LancerSettings
        {
            get { return WindowSettings as LancerSetting; }
        }
        public sealed override ClassJobType[] Class
        {
            get
            {
                return new [] 
                {  ClassJobType.Dragoon
                };
            }
        }
        [Behavior(BehaviorType.PreCombatBuffs, ClassJobType.Dragoon)]
        public Composite DragoonCreateBasicPreCombatBuffs()
        {
            return new ActionRunCoroutine(ctx => DragoonBuildPreCombatBuffs());
        }
        public static async Task<bool> DragoonBuildPreCombatBuffs()
        {
            if (!IsValid()) return false;
            if (Resting)
            {
                if (!Core.Player.IsCasting && await Spell.Cast("Cure", Core.Player, Core.Player.CurrentHealthPercent < 70 && Core.Player.CurrentHealthPercent < Core.Player.CurrentManaPercent)) return true;
                if (!Core.Player.IsCasting && await Spell.Cast("Physick", Core.Player, Core.Player.CurrentHealthPercent < 70 && Core.Player.CurrentHealthPercent < Core.Player.CurrentManaPercent)) return true;
                return false;
            }
            if (await Spell.BuffParty("Protect")) return true;
            if (await Spell.BuffParty("Stoneskin")) return true;
            return await CombatCompanion();
        }
        [Behavior(BehaviorType.Rest, ClassJobType.Dragoon)]
        public Composite DragoonCreateBasicRest()
        {
            return new ActionRunCoroutine(ctx => DefaultRestBehavior(Core.Player.CurrentTPPercent));
        }
        [Behavior(BehaviorType.CombatBuffs, ClassJobType.Dragoon)]
        public Composite DragoonCreateBasicCombatBuffs()
        {
            return new ActionRunCoroutine(ctx => DragoonBuildCombatBuffs()); 
        }
        public static async Task<bool> DragoonBuildCombatBuffs()
        {   //add elusive jump
            ShouldPulse = true;
            if (await Spell.Cast("Cure", Core.Player, Core.Player.CurrentHealthPercent < 40)) return true;
            if (await Spell.Cast("Physick", Core.Player, Core.Player.CurrentHealthPercent < 40)) return true;
            if (Spell.GcdTime < LancerSettings.NoneGcdWindowEnd) return false;
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Invigorate", Core.Player, Core.Player.CurrentTPPercent < 50))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Eye for an Eye", Core.Player, true))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Foresight", Core.Player, !Core.Player.HasAura("Foresight")))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Blood for Blood", Core.Player, true))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Internal Release", Core.Player, true))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            if (WindowSettings.CooldownsUsable && Spell.Potion != null && Spell.Potion.IsValid
                && Spell.Potion.CanUse(Core.Player) && Spell.Potion.Name.Equals(WindowSettings.PotionName))
            {
                Spell.Potion.UseItem(Core.Player);
                if (WindowSettings.PotionForce) Spell.UsedNoneGcd = true;
            }
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Quelling Strikes", Core.Player, true))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Hawk's Eye", Core.Player))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Bloodbath", Core.Player, true))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Raging Strikes", Core.Player, true))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Keen Flurry", Core.Player, Core.Player.CurrentHealthPercent < 80))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Foresight", Core.Player, Core.Player.CurrentHealthPercent < 60))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Convalescence", Core.Player, Core.Player.CurrentHealthPercent < 70))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Second Wind", Core.Player, Core.Player.CurrentHealthPercent < 40))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Awareness", Core.Player, Core.Player.CurrentHealthPercent < 30))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Featherfoot", Core.Player, Core.Player.Distance2D() < 5))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            return false;
        }
        [Behavior(BehaviorType.Pull, ClassJobType.Dragoon)]
        public Composite DragoonCreateBasicPull()
        {
            return new ActionRunCoroutine(ctx => DragoonBuildPullBehavior());
        }
        public static async Task<bool> DragoonBuildPullBehavior()
        {
            if (Core.Player.CurrentTarget != null && !Core.Player.CurrentTarget.CanAttack || !IsValid()) return false;
            ShouldPulse = true;
            await Target.EnsureValidTarget();
            if (await Movement.PullMove(Core.Player.CurrentTarget)) return true;
            if (LancerSettings.WaitEnergyPull && Core.Player.CurrentTPPercent < LancerSettings.RestEnergy)
            {
                ff14bot.Helpers.Logging.Write("[RebornCo] Waiting for energy before pull!");
                return true;
            }
            if (!Spell.Gcd && await Spell.PullCast("Vorpal Thrust", Core.Player.CurrentTarget, Spell.LastCombo == Combo.TrueThrust )) return true;
            if (!Spell.Gcd && await Spell.PullCast("Full Thrust", Core.Player.CurrentTarget, Spell.LastCombo == Combo.VorpalThrust )) return true;
            if (!Spell.Gcd && await Spell.PullCast("Disembowel", Core.Player.CurrentTarget, Spell.LastCombo == Combo.ImpulseDrive )) return true;
            if (!Spell.Gcd && await Spell.PullCast("Chaos Thrust", Core.Player.CurrentTarget, Spell.LastCombo == Combo.Disembowel)) return true;
            if (!Spell.Gcd && await Spell.CastLock("Heavy Thrust", Core.Player.CurrentTarget, !Core.Player.HasAura("Heavy Thrust", false, LancerSettings.HeavyThrustClip))) return true;
            return await Spell.PullCast("True Thrust", Core.Player.CurrentTarget);
        }
        [Behavior(BehaviorType.Combat, ClassJobType.Dragoon)]
        public Composite DragoonCreateBasicCombat()
        {
            return new ActionRunCoroutine(ctx => DragoonBuildBasicCombat());
        }
        public static async Task<bool> DragoonBuildBasicCombat()
        {
            await Target.EnsureValidTarget();
            await Movement.MoveToTarget();
            if (Core.Player.CurrentTarget != null && ff14bot.Managers.MovementManager.IsMoving
                && !(Core.Player.CurrentTarget.InLineOfSight() && Core.Player.CurrentTarget.Distance2D() < Movement.ObjectMeleeRange(Core.Player.CurrentTarget))) return true;
            if (!Spell.Gcd && Spell.LastCombo == Combo.ImpulseDrive && Core.Player.ClassLevel > 37)
            {
                return await Spell.Cast("Disembowel", Core.Player.CurrentTarget);
            }
            if (!Spell.Gcd && Spell.LastCombo == Combo.Disembowel
                && Core.Player.ClassLevel == 50)
            {
                return await Spell.Cast("Chaos Thrust", Core.Player.CurrentTarget); //move behind
            }
            if (!Spell.Gcd && Spell.LastCombo == Combo.TrueThrust)
            {
                return await Spell.Cast("Vorpal Thrust", Core.Player.CurrentTarget);
            }
            if (!Spell.Gcd && Spell.LastCombo == Combo.VorpalThrust 
                && Core.Player.ClassLevel > 25)
            {
                return await Spell.Cast("Full Thrust", Core.Player.CurrentTarget);
            }
            if (!Spell.Gcd && await Spell.CastLock("Heavy Thrust", Core.Player.CurrentTarget, !Core.Player.HasAura("Heavy Thrust", false, LancerSettings.HeavyThrustClip))) return true;
            if (!Spell.Gcd && await Spell.CastLock("Impulse Drive", Core.Player.CurrentTarget, Core.Player.CurrentTarget != null && Core.Player.ClassLevel > 37 
                && !Core.Player.CurrentTarget.HasAura("Disembowel", false, LancerSettings.DisembowelClip))) return true;
            if (Core.Player.CurrentTarget != null
                && Core.Player.CurrentTarget.Distance2D() > Movement.MeleeRange)
            {
                if (await Spell.CastLock("Power Surge", Core.Player, Spell.CanCast(Spell.GetSpellData("Jump"), Core.Player.CurrentTarget))) return true;
                if (await Spell.CastLocation("Dragonfire Dive", Core.Player.CurrentTarget, WindowSettings.Aoe && Target.EnemiesNearTarget(5) >= WindowSettings.AoeCount,false,true)) return true;
                if (await Spell.CastLock("Spineshatter Dive", Core.Player.CurrentTarget)) return true;
                if (await Spell.CastLock("Jump", Core.Player.CurrentTarget)) return true;
            }
            if (!Spell.Gcd && await Spell.CastLock("Phlebotomize", Core.Player.CurrentTarget, !Core.Player.CurrentTarget.HasAura("Phlebotomize", true, LancerSettings.PhlebotomizeClip))) return true;
            if (!Spell.Gcd && await Spell.Cast("Piercing Talon", Core.Player.CurrentTarget, Core.Player.CurrentTarget != null && Core.Player.CurrentTarget.Distance2D() < 15 && Core.Player.CurrentTarget.Distance2D() > (Movement.MeleeRange + LancerSettings.DistanceFromMeleeRangeToUseRanged))) return true;
            if (!Spell.Gcd && await Spell.CastLock("Virus", Core.Player.CurrentTarget, Core.Player.CurrentTarget != null && !Core.Player.CurrentTarget.HasAura("Virus"))) return true;
            if (!Spell.Gcd && await Spell.CastLock("Venomous Bite", Core.Player.CurrentTarget, !Core.Player.CurrentTarget.HasAura("Venomous Bite", true))) return true;
            if (!Spell.Gcd && await Spell.CastLock("Fracture", Core.Player.CurrentTarget, Core.Player.CurrentTarget != null && !Core.Player.CurrentTarget.HasAura("Fracture"))) return true;
            if (!Spell.Gcd && await Spell.Cast("True Thrust", Core.Player.CurrentTarget)) return true;
            if (Spell.GcdTime < LancerSettings.NoneGcdWindowEnd) return false;
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Life Surge", Core.Player, Spell.LastCombo == Combo.VorpalThrust))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Mercy Stroke", Core.Player.CurrentTarget, Core.Player.CurrentTarget != null && Core.Player.CurrentTarget.CurrentHealthPercent < 20))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            if (!Spell.UsedNoneGcd && await Spell.CastLock("Leg Sweep", Core.Player.CurrentTarget, LancerSettings.UseLegSweep))
            {
                Spell.UsedNoneGcd = true;
                return true;
            }
            return false;
        }
    }
}